package com.da.auth.service.impl;

import com.da.common.entity.PageResult;
import com.da.common.enums.ResultEnum;
import com.da.common.exception.BusinessException;
import com.da.common.utils.SnowflakeIdWorker;
import com.da.common.utils.TimestampUtil;
import com.da.auth.entity.DailyPermissions;
import com.da.auth.mapper.CommonMapper;
import com.da.auth.mapper.DailyPermissionsMapper;
import com.da.auth.service.DailyPermissionsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 权限表(DailyPermissions)表服务实现类
 *
 * @author makejava
 * @since 2025-01-21 09:47:43
 */
@Service
@Slf4j
public class DailyPermissionsServiceImpl implements DailyPermissionsService {

    private final DailyPermissionsMapper dailyPermissionsMapper;



    private final CommonMapper<?> commonMapper;

    public DailyPermissionsServiceImpl(DailyPermissionsMapper dailyPermissionsMapper, CommonMapper<?> commonMapper) {
        this.dailyPermissionsMapper = dailyPermissionsMapper;
        this.commonMapper = commonMapper;
    }


    @Override
    public Boolean addPermission(DailyPermissions dailyPermissions) {
        String permissionName = dailyPermissions.getPermissionName();
        if (StringUtils.isBlank(permissionName)) {
            log.error("该权限名称不能为空");
            throw new BusinessException(ResultEnum.FAIL.getCode(), "该权限名称不能为空");
        }
        int count = commonMapper.queryCountByCondition("daily_permission", "permission_name", permissionName);
        if (count > 0) {
            log.error("该权限名称已重复");
            throw new BusinessException(ResultEnum.FAIL.getCode(), "该权限名称已重复");
        }
        dailyPermissions.setId(String.valueOf(SnowflakeIdWorker.generateId()));
        dailyPermissions.setCreatedAt(TimestampUtil.now());
        try {
            int isAddPermission = dailyPermissionsMapper.addPermission(dailyPermissions);
            return isAddPermission > 0;
        } catch (Exception e) {
            log.error("新增权限失败");
            throw new BusinessException(ResultEnum.FAIL.getCode(), e.getMessage());
        }
    }

    @Override
    public PageResult<DailyPermissions> queryPageByPermission(int pageNum, int pageSize) {
        // 计算偏移量
        int offset = (pageNum - 1) * pageSize;
        List<DailyPermissions> all = dailyPermissionsMapper.selectPage(offset, pageSize);
        int total = dailyPermissionsMapper.selectCount();
        return new PageResult<>(pageNum, pageSize, total, all);
    }

    @Override
    public DailyPermissions queryPermissionInfoById(String id) {
        return dailyPermissionsMapper.selectById(id);
    }

    @Override
    public Boolean deletedPermission(String id) {
        DailyPermissions dailyPermission = (DailyPermissions) commonMapper.queryDataById("daily_permission", id);
        if (ObjectUtils.isEmpty(dailyPermission)) {
            log.error("未查询到该id为:" + id + "的权限");
            throw new BusinessException(ResultEnum.FAIL.getCode(), "未找到 ID 为 " + id + " 的权限");
        }
        try {
            int isDeletedRole = dailyPermissionsMapper.deleteById(id);
            return isDeletedRole > 0;
        } catch (Exception e) {
            log.error("删除权限失败:{}", e.getMessage());
            throw new BusinessException(ResultEnum.FAIL.getCode(), e.getMessage());
        }
    }


}
